package com.open.launcher.view;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;


/**
 * Created by Shinelon on 2017/10/25.
 */

public class HappyTextView extends RelativeLayout {
    private TextView textView;
    private Context mContext;
    private int viewW;
    private int textW;

    public HappyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d("tag", "-----------context--------" + mContext);
        this.mContext = context;
        initView();
    }

    private void initView() {
        textView = new TextView(mContext);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textView.setLayoutParams(params);
        addView(textView);
    }


    public void setText(String text) {
        textView.setText(text);
        textView.setTextColor(Color.parseColor("#ffffff"));
    }

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        viewW = getWidth();
        textW = textView.getWidth();
        textView.setVisibility(INVISIBLE);
    }

    public void startAnim() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "translationX", viewW, -textW);
        animator.setDuration(10000);
        animator.start();
        animator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                textView.setVisibility(VISIBLE);
                handler.sendEmptyMessageDelayed(0, 0);
            }
        });
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            startAnim();
        }
    };
}
